package persistence;

import gui.GuiController;

import java.util.List;

import domain.Game;
import domain.Player;
import domain.Tournament;

public class PersistenceController {

	private static PersistenceController persistenceController;
	
	private XMLHandler<List<Game>> gamesXMLHandler;
	private XMLHandler<List<Player>> playersXMLHandler;
	private XMLHandler<Tournament> tournamentsXMLHandler;
	
	private PersistenceController() {
		this.gamesXMLHandler = new GamesXMLHandler();
		this.playersXMLHandler = new PlayersXMLHandler();
		this.tournamentsXMLHandler = new TournamentsXMLHandler();
	}

	public Tournament openTournament(String name) {
		Tournament openedTournament = this.tournamentsXMLHandler.openTournament(name);
		List<Game> correspondingGames = this.gamesXMLHandler.openTournament(name);
		List<Player> correspondingPlayers = this.playersXMLHandler.openTournament(name);
		
		openedTournament.setGames(correspondingGames);
		openedTournament.setRegisteredPlayers(correspondingPlayers);
		return openedTournament;
	}

	public String[] getMostRecentTournaments() {
		return ((TournamentsXMLHandler) this.tournamentsXMLHandler).getMostRecentTournaments();
	}

	public void saveTournament(Tournament tournament) {
		this.tournamentsXMLHandler.saveTournament(tournament, tournament.getName());
		this.gamesXMLHandler.saveTournament(tournament.getGames(), tournament.getName());
		//this.playersXMLHandler.saveTournament(tournament.getPlayers(), tournament.getName());
	}

	public static PersistenceController getInstance() {
		if (persistenceController == null) {
			persistenceController = new PersistenceController();
		}
		return persistenceController;
	}

	public boolean isTournamentNameUnique(String name) {
		return ((TournamentsXMLHandler) this.tournamentsXMLHandler).isTournamentNameUnique(name);
	}
	
	

	
}